//
//  TestVC.swift
//  KYC2C
//
//  Created by Guanghui Liao on 12/11/17.
//  Copyright © 2017 liaoworking. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
private let reuseIdentifier = "Cell"


class TestCell:UICollectionViewCell{
    
    
    var titleText: String? {
        didSet{
            self.titleLabel.text = titleText
            self.titleLabel.sizeToFit()
            
        }
    }
    
    lazy var titleLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI()  {
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        titleLabel.textColor = UIColor.black
        titleLabel.text = titleText
        titleLabel.frame = CGRect(x: 0, y: self.frame.size.width / 2, width: 0, height: 0)
        
        self.contentView.addSubview(titleLabel)
        self.contentView.backgroundColor = UIColor.white
    }
}

class TestVC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    static let disposeBag = DisposeBag()

    lazy var collectionView = UICollectionView()
    
    lazy var dataList = { () -> Array<Dictionary<String, Any>>? in
        
        return[["title":"接口测试",
                "className":"APITestVC"],
               ["title":"doubleTable",
                "className":"GHBaseDoubleTableVC"],
               ["title":"基础功能方法测试",
                "className":"BaseFuncTestVC"],
               ["title":"测试付款弹窗",
                "className":"testPayingVC"],
               ["title":"H5风险评测",
                "className":"TestRiskTestVC"],
               ["title":"添加银行卡",
                "className":"GHAddBankCardViewController"],
               ["title":"互保团信息",
                "className":"GHMutualWarrantTeamInfoVC"],
               ["title":"委托催收",
                "className":"GHEntrustDunningVC"],
               ["title":"测试dropdown",
                "className":"TestDropDownVC"],
               ["title":"图片选择测试",
                "className":"TestImagePickVC"],
               ["title":"图片选择测试",
                "className":"YMImgPickerVC"],
               ["title":"最后图选",
                "className":"TestNewImagePicker"]
        ]
    }()
    
    /// 把触发按钮放在首页的tabbar上
    class func showOnHomePage(){
        //按照传统的架构，一般都是tab+Nav形式的
        if isDebug {
            let rootVC = UIApplication.shared.keyWindow?.rootViewController
            let testBtn = UIButton(frame: CGRect(x: 80, y: 20, width: 80, height: 30))
            testBtn.backgroundColor = UIColor.red
            testBtn.setTitle("TEST", for: .normal)
            testBtn.rx.tap.subscribe(onNext: {
                let nav = GHNavVC(rootViewController: TestVC())
                rootVC?.present(nav, animated: true, completion: nil)
            }).disposed(by: disposeBag)
            
            rootVC?.view.addSubview(testBtn)
        }
    }
    
    @objc func testClikc(sender:UIButton) {
        print("testClick")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCollectionView()
        configRightItem()
    }
    
    func setupCollectionView() {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width/4.01, height: UIScreen.main.bounds.width/4.01)
        
        self.collectionView = UICollectionView(frame: UIScreen.main.bounds, collectionViewLayout: flowLayout)
        self.collectionView.backgroundColor = UIColor.lightGray;
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.frame = UIScreen.main.bounds
        view.addSubview(self.collectionView)
        // Register cell classes
        self.collectionView.register(TestCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    func configRightItem() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(fakePopBack))
    }
    
   @objc func fakePopBack() {
        self.dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: UICollectionViewDataSource
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.dataList!.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let dataDict = self.dataList![indexPath.row] as Dictionary
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TestCell
        
        cell.titleText = (dataDict["title"]! as? String)!
        
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dataDict = self.dataList![indexPath.row] as Dictionary
        let className = (dataDict["className"]! as? String)!
        
        //能获取到appName的情况下才去跳转 类名转化为实体类
        if  let appName: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String? {
            // generate the full name of your class (take a look into your "YourProject-swift.h" file)
            let classStringName = "\(appName).\(className)"
            
            let classType = NSClassFromString(classStringName) as? UIViewController.Type
            if let type = classType {
                let newVC = type.init()
                navigationController?.pushViewController(newVC, animated: true)
                
            }
        }
    }
}
